class Solution {
    public int lastStoneWeight(int[] stones) {
        if(stones.length == 1) return stones[0];
        int n = stones.length;
        Arrays.sort(stones);
        while(true){
            if(stones[n-1] == stones[n-2]){
                if(stones[n-1] == 0) return 0;
                stones[n-1] = 0;
                stones[n-2] = 0;
                Arrays.sort(stones);
            }else{
                if(stones[n-2] == 0) return stones[n-1];
                int val = stones[n-1] - stones[n-2];
                stones[n-1] = 0;
                stones[n-2] = val;
                Arrays.sort(stones);
            }
        }
    }
}

